package com.wgts.wgts.mvp.view.activity.search;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.viewpage.ComFragmentAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchResultActivity extends BaseActivity {
    @BindView(R.id.img_search)
    ImageView imgSearch;
    @BindView(R.id.img_clear)
    ImageView imgClear;
    @BindView(R.id.edit_search)
    TextView editSearch;
    @BindView(R.id.cancle)
    LinearLayout cancle;
    @BindView(R.id.tv_title_goods)
    TextView tvTitleGoods;
    @BindView(R.id.view_title_goods)
    View viewTitleGoods;
    @BindView(R.id.tv_title_shops)
    TextView tvTitleShops;
    @BindView(R.id.view_title_shops)
    View viewTitleShops;
    @BindView(R.id.view_page)
    ViewPager viewPage;
    @BindView(R.id.shop_ly)
    LinearLayout shopLy;
    @BindView(R.id.store_ly)
    LinearLayout storeLy;

    @Override
    public void init() {
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.search_result_activity);
        ButterKnife.bind(this);
        initView();
    }


    private void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        Bundle bundle = new Bundle();
        bundle.putString("name", getIntent().getStringExtra(SearchActivity.EDITTEXT));
        fragments.add(SearchResultGoodsFragment.getInstance(bundle));
        bundle = new Bundle();
        bundle.putString("name", getIntent().getStringExtra(SearchActivity.EDITTEXT));
        fragments.add(SearchResultStoreFragment.getInstance(bundle));
        viewPage.setAdapter(new ComFragmentAdapter(getSupportFragmentManager(), fragments));
        String tmp = getIntent().getStringExtra(SearchActivity.EDITTEXT);
        editSearch.setText(getIntent().getStringExtra(SearchActivity.EDITTEXT));


        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                if (i == 0) {
                    viewTitleGoods.setVisibility(View.VISIBLE);
                    viewTitleShops.setVisibility(View.GONE);
                } else {
                    viewTitleShops.setVisibility(View.VISIBLE);
                    viewTitleGoods.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @OnClick({R.id.edit_search, R.id.cancle, R.id.shop_ly, R.id.store_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_search:
                finish();
                break;
            case R.id.cancle:
                finish();
                break;
            case R.id.shop_ly:
                viewPage.setCurrentItem(0);
                viewTitleGoods.setVisibility(View.VISIBLE);
                viewTitleShops.setVisibility(View.INVISIBLE);
                break;
            case R.id.store_ly:
                viewPage.setCurrentItem(1);
                viewTitleShops.setVisibility(View.VISIBLE);
                viewTitleGoods.setVisibility(View.INVISIBLE);
                break;
        }
    }

}
